﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace TurtleEngine.Helper.CHMWriter
{
	 /// <summary>
	 ///
	 /// </summary>
	 public class HHPWriter
	 {
		  /// <summary>
		  /// The files
		  /// </summary>
		  private readonly List<HTMLFile> files = new List<HTMLFile>();

		  /// <summary>
		  /// The info types
		  /// </summary>
		  private readonly List<HTMLFile> infoTypes = new List<HTMLFile>();

		  /// <summary>
		  /// The tw
		  /// </summary>
		  private TextWriter tw;

		  /// <summary>
		  /// Sets the add file.
		  /// </summary>
		  /// <value>
		  /// The add file.
		  /// </value>
		  public HTMLFile AddFile
		  {
				set { files.Add(value); }
		  }

		  /// <summary>
		  /// Sets the type of the add info.
		  /// </summary>
		  /// <value>
		  /// The type of the add info.
		  /// </value>
		  public HTMLFile AddInfoType
		  {
				set { infoTypes.Add(value); }
		  }

		  /// <summary>
		  /// Gets or sets the compatibility.
		  /// </summary>
		  /// <value>
		  /// The compatibility.
		  /// </value>
		  public string Compatibility { get; set; }

		  /// <summary>
		  /// Gets or sets the compiled file.
		  /// </summary>
		  /// <value>
		  /// The compiled file.
		  /// </value>
		  public string CompiledFile { get; set; }

		  /// <summary>
		  /// Gets or sets the contents file.
		  /// </summary>
		  /// <value>
		  /// The contents file.
		  /// </value>
		  public string ContentsFile { get; set; }

		  /// <summary>
		  /// Gets or sets the default topic HTML file.
		  /// </summary>
		  /// <value>
		  /// The default topic HTML file.
		  /// </value>
		  public string DefaultTopicHTMLFile { get; set; }

		  /// <summary>
		  /// Gets or sets the index file.
		  /// </summary>
		  /// <value>
		  /// The index file.
		  /// </value>
		  public string IndexFile { get; set; }

		  /// <summary>
		  /// Gets or sets the language.
		  /// </summary>
		  /// <value>
		  /// The language.
		  /// </value>
		  public string Language { get; set; }

		  /// <summary>
		  /// Gets or sets the output file.
		  /// </summary>
		  /// <value>
		  /// The output file.
		  /// </value>
		  public string OutputFile { get; set; }

		  /// <summary>
		  /// Writes this instance.
		  /// </summary>
		  public void Write()
		  {
				#region sample file

				//[OPTIONS]
				//Compatibility=1.1 or later
				//Compiled file=AdventureWorks.chm
				//Contents file=Table of Contents.hhc
				//Default topic=D:\Amit\myWork\SQLDoc\SQLDoc\SQLDoc\bin\Debug\html\Root.htm
				//Index file=Index.hhk
				//Language=0x409 English (United States)

				//[FILES]
				//D:\Amit\myWork\SQLDoc\SQLDoc\SQLDoc\bin\Debug\html\TableList.htm
				//D:\Amit\myWork\SQLDoc\SQLDoc\SQLDoc\bin\Debug\html\Address.htm

				//[INFOTYPES]

				#endregion sample file

				using (tw = new StreamWriter(OutputFile))
				{
					 tw.WriteLine(WriteOptions());
					 tw.WriteLine(WriteFiles());
					 tw.WriteLine(WriteInfoTypes());

					 tw.Flush();
				}
		  }

		  /// <summary>
		  /// Writes the files.
		  /// </summary>
		  /// <returns></returns>
		  private string WriteFiles()
		  {
				string fileName = "[FILES]" + Environment.NewLine;

				foreach (HTMLFile file in files)
				{
					 fileName += file.Local + Environment.NewLine;
				}
				return fileName + Environment.NewLine;
		  }

		  /// <summary>
		  /// Writes the info types.
		  /// </summary>
		  /// <returns></returns>
		  private string WriteInfoTypes()
		  {
				string infoTypeName = "[INFOTYPES]" + Environment.NewLine;

				foreach (HTMLFile infoType in infoTypes)
				{
					 infoTypeName += infoType.Local + Environment.NewLine;
				}
				return infoTypeName + Environment.NewLine;
		  }

		  /// <summary>
		  /// Writes the options.
		  /// </summary>
		  /// <returns></returns>
		  private string WriteOptions()
		  {
				string location = Assembly.GetExecutingAssembly().Location;
				location = location.Substring(0, location.LastIndexOf("\\")) + "\\html\\";

				string options = "[OPTIONS]" + Environment.NewLine;

				options += "Compatibility = " + (String.IsNullOrEmpty(Compatibility) ? "1.1 or later" : Compatibility) +
							Environment.NewLine;
				options += "Compiled file=" + (String.IsNullOrEmpty(CompiledFile) ? "AdventureWorks.chm" : CompiledFile) +
							Environment.NewLine;
				options += "Contents file=" + (String.IsNullOrEmpty(ContentsFile) ? "Table of Contents.hhc" : ContentsFile) +
							Environment.NewLine;
				options += "Default topic=" +
							(String.IsNullOrEmpty(DefaultTopicHTMLFile) ? location + "Root.htm" : DefaultTopicHTMLFile) + Environment.NewLine;

				//options += "Index file=" + (String.IsNullOrEmpty(IndexFile) ? "Index.hhk" : IndexFile) + Environment.NewLine;
				options += "Language=" + (String.IsNullOrEmpty(Language) ? "0x409 English (United States)" : Language) +
							Environment.NewLine;

				return options + Environment.NewLine;
		  }
	 }
}